<?php include "conn.php";
$zone_id=mysqli_real_escape_string($conn,$_GET['zone_id']);
$parent_id=mysqli_real_escape_string($conn,$_GET['parent_id']);
$page = isset($_GET['page']) ? $_GET['page'] : 1; 
$perpage = 10;

if($parent_id==0)
{
	$result = mysqli_query($conn, "call sp_search_waimai({$perpage}, {$page}, 1, $zone_id,0,0,'')");
	while($row = mysqli_fetch_array($result))
	{
	    $data[] = $row;
	}
}
else
{
	$result = mysqli_query($conn, "call sp_search_waimai(1000, 1,1,$parent_id,$zone_id,0,'')");
	while($row = mysqli_fetch_array($result))
	{
	    $data[] = $row;
	}	
}
			
?>
<?php include "header.php"; ?>
<body>
<?php include "body_top.php";?>
      <table class="restaurant-list-table">
        <?php
		if($data)
		{
			foreach($data as $row){
		?>
			<tr class="open">
          		<td class="logo-wrapper" valign="top"><a href="/waimai/dish.php?shop_id=<?=$row['shop_id'];?>" title="<?=$row['name'];?>"> <img title="<?=$row['name'];?>" alt="<?=$row['name'];?>" src="http://img2.sudingke.com/upload/<?=str_replace( ".jpg", "-80x56.jpg 
",$row['picture']);?>" /> </a></td>
          		<td class="info"><div class="name"><a href="/waimai/dish.php?shop_id=<?=$row['shop_id'];?>">
            	<?= $row['name'];?></a> <span class="flavor"><?=$row['address'];?></span></div>
            	<div class="status"><span>送餐时间:</span><span class="deliver-amount"><?  $list_data=(unserialize($row['list_data'])); 
				echo $list_data['delivery_time'];?></span> </div>
            	<div class="description"><span>配送范围:</span><?=$list_data['delivery_desc'];?></div>
            	</td>
        	</tr>
        <?php 
			} }
		?>
      </table>
 <?
 echo pages($row[0], $page, $perpage, '?zone_id='.$zone_id.'&parent_id='.$parent_id.'&page={PAGE}', 10);
function pages($total, $curr_page, $perpage = 20, $urlrule = '?page={PAGE}', $pagesets = 10) {
    $multipage = '';
    $page = $offset = $pages = 0;
    if($total > $perpage) {
        $page = $pagesets + 1;
        $offset = ceil($pagesets / 2 - 1);
        $pages  = ceil($total / $perpage);

        $from = $curr_page - $offset;
        $to   = $curr_page + $offset;
        $more = 0;
        if($page >= $pages) {
            $from = 2;
            $to   = $pages - 1;
        } else {
            if($from <= 1) {
                $to   = $page - 1;
                $from = 2;
            }  elseif($to >= $pages) {
                $from = $pages - ( $page - 2 );
                $to   = $pages - 1;
            }
            $more = 1;
        }
        $multipage .= '<a class="a1">'.$total.'条</a>';
        if($curr_page>0) {
            $multipage .= ' <a href="'.pageurl($urlrule, $curr_page-1).'" class="a1">上一页</a>';
            if($curr_page==1) {
                $multipage .= ' <span>1</span>';
            } elseif($curr_page>6 && $more) {
                $multipage .= ' <a href="'.pageurl($urlrule, 1).'">1</a> ... ';
            } else {
                $multipage .= ' <a href="'.pageurl($urlrule, 1).'">1</a>';
            }
        }
        for($i = $from; $i <= $to; $i++) {
            if($i != $curr_page) {
                $multipage .= ' <a href="'.pageurl($urlrule, $i).'">'.$i.'</a>';
            } else {
                $multipage .= ' <span>'.$i.'</span>';
            }
        }
        if($curr_page < $pages) {
            if($curr_page < $pages - 5 && $more) {
                $multipage .= ' ... <a href="'.pageurl($urlrule, $pages).'">'.$pages.'</a> <a href="'.pageurl($urlrule, $curr_page + 1).'" class="a1">下一页</a>';
            } else {
                $multipage .= ' <a href="'.pageurl($urlrule, $pages).'">'.$pages.'</a> <a href="'.pageurl($urlrule, $curr_page + 1).'" class="a1">下一页</a>';
            }
        } elseif($curr_page == $pages) {
            $multipage .= ' <span>'.$pages.'</span> <a href="'.pageurl($urlrule, $curr_page).'" class="a1">下一页</a>';
        } else {
            $multipage .= ' <a href="'.pageurl($urlrule, $pages).'">'.$pages.'</a> <a href="'.pageurl($urlrule, $curr_page + 1).'" class="a1">下一页</a>';
        }
    }
    return $multipage;
}

/**
 * 拼接分页链接
 *
 * @return integer
 */
function pageurl($urlrule, $page) {
    return str_replace('{PAGE}', $page, $urlrule);
}

?>
 
<?php include "footer.php";?>
